package com.ebelter.btcomlib.views.dialogs.otherpick;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.utils.DipUtil;


import com.ebelter.btcomlib.views.dialogs.picktime.OnChangeLisener;
import com.ebelter.btcomlib.views.dialogs.picktime.OnSureLisener;

import java.util.Date;


public class TianCiPickDialog extends Dialog implements OnChangeLisener {


    private FrameLayout wheelLayout;

    private TextView sure;
    private TextView cancel;







    private OnChangeLisener onChangeLisener;

    private OnSureLisener onSureLisener;

    private TianCiPicker mDatePicker;





    //设置选择回调
    public void setOnChangeLisener(OnChangeLisener onChangeLisener) {
        this.onChangeLisener = onChangeLisener;
    }

    //设置点击确定按钮，回调
    public void setOnSureLisener(OnSureLisener onSureLisener) {
        this.onSureLisener = onSureLisener;
    }

    public TianCiPickDialog(Context context) {
        super(context, R.style.dialog_style);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cbk_dialog_pick_time);

        initView();
        initParas();
    }

    private TianCiPicker getPicker() {
        TianCiPicker picker = new TianCiPicker(getContext());

        picker.setOnChangeLisener(this);
        picker.init();

        return picker;
    }


    public void setCurrentIndex(int index){
        mDatePicker.setCurrentItem(index);
    }



    private void initView() {
        this.sure =  findViewById(R.id.sure);
        this.cancel =  findViewById(R.id.cancel);

        this.wheelLayout = findViewById(R.id.wheelLayout);



        mDatePicker = getPicker();
        this.wheelLayout.addView(mDatePicker);




        this.sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (onSureLisener != null) {
                    onSureLisener.onSure(null,mDatePicker.getSelectIndex());
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initParas() {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = DipUtil.getWindowWidth(getContext());
        getWindow().setAttributes(params);
    }

    @Override
    public void onChanged(Date date,int index) {

        if (onChangeLisener != null) {
            onChangeLisener.onChanged(date,index);
        }


    }


}
